﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ToastTips : TipsBase {

    
    public int Priority { get; set; }

    bool m_IsShowing;
    public bool isShowing { get { return m_IsShowing; } set { m_IsShowing = value; } }

    ZTransformAnim _mZTransformAnim;
    ZTransformAnim zTransformAnim
    {
        get
        {
            if(_mZTransformAnim == null)
            {
                _mZTransformAnim = gameObject.AddComponent<ZTransformAnim>();
            }
            return _mZTransformAnim;
        }
    }

    public AnimationCurve alphaFadeInCurve;
    public AnimationCurve alphaFadeOutCurve;
    public AnimationCurve scaleFadeInCurve;
    public AnimationCurve scaleFadeOutCurve;

    public UnityEngine.GameObject fxPref;

    // Use this for initialization
    protected override void Awake () {

        base.Awake();

        if(fxPref)
            fxPref.gameObject.SetActive(false);
    }

    public  override void SetContent(string content)
    {
        base.SetContent(content);
    }

    [ContextMenu("Show")]
    public void Show()
    {
        isShowing = true;
        MZuiAnimTools.InitCurve(this.alphaFadeInCurve);
        MZuiAnimTools.SetImgTxtAlpha(this.GetComponent<RectTransform>(), 0);
        const float fadeInTime = 0.2f;
        MZuiAnimTools.UIAlpha(this.GetComponent<RectTransform>(), 1, fadeInTime, OnShowEnd, ZUIAnimTools.EaseType.Curve);

        transform.localScale = Vector3.zero;
        zTransformAnim.SetCurve(scaleFadeInCurve);
        zTransformAnim.ScaleTo(transform, Vector3.one, fadeInTime, null);

        if (/*PanelCommon.uiFXCamera && */fxPref)
        {
            var fx = Instantiate(fxPref);
            fx.gameObject.SetActive(true);

            // var worldPos = Game.Instance.uiFXCamera.ViewportToWorldPoint(mousePosition);
            fx.transform.position = transform.position;// worldPos;
            fx.AddComponent<DelayDestroy>().lifeTime = 5;
        }
    }

    void OnShowEnd()
    {

    }

    [ContextMenu("Hide")]
    public void Hide()
    {
        isShowing = false;
        const float fadeOutTime = 0.4f;
        MZuiAnimTools.InitCurve(this.alphaFadeOutCurve);
        MZuiAnimTools.UIAlpha(this.GetComponent<RectTransform>(), 0, fadeOutTime, OnHideEnd, ZUIAnimTools.EaseType.Curve);
        
        zTransformAnim.SetCurve(scaleFadeOutCurve);
        zTransformAnim.ScaleTo(transform, Vector3.one * 1.5f, fadeOutTime, null);
    }
    
    void OnHideEnd()
    {
        Destroy(gameObject);
    }

    // Update is called once per frame
    void Update () {
		
	}
}
